﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace War3Trainer
{
    public partial class OverlayForm : Form
    {
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

        public OverlayForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.Manual;
            this.FormBorderStyle = FormBorderStyle.None;
            this.TopMost = true;
            this.BackColor = Color.LimeGreen; // 方便调试
            this.TransparencyKey = Color.LimeGreen; // 透明
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // 1. 查找魔兽争霸窗口（请用实际窗口标题）
            IntPtr war3Hwnd = FindWindow(null, "Warcraft III");
            if (war3Hwnd == IntPtr.Zero)
            {
                MessageBox.Show("未找到魔兽争霸窗口，请先启动游戏！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
                return;
            }

            // 2. 获取魔兽窗口大小和位置
            RECT rect;
            if (GetWindowRect(war3Hwnd, out rect))
            {
                int width = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;
                this.Location = new Point(rect.Left, rect.Top);
                this.Size = new Size(width, height);
            }

            // 3. 设置父窗口
            SetParent(this.Handle, war3Hwnd);

            // 4. 移动窗口到魔兽窗口区域
            MoveWindow(this.Handle, 0, 0, this.Width, this.Height, true);
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
    }
}